package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class VIewSearchServlet extends HttpServlet {
    public static final String VIEW_SEARCH= "/WEB-INF/jsp/catalog/SearchProducts.jsp";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String keyword=  req.getParameter("keyword");
        CatalogService service = new CatalogService();
        List<Product> products= service.searchProductList(keyword);

        HttpSession session= req.getSession();
        session.setAttribute("products", products);

        req.getRequestDispatcher(VIEW_SEARCH).forward(req, resp);
    }


}
